/*
 * Copyright (c) 2017 maoyan.com
 * All rights reserved.
 *
 */
package com.darren.euc.util;

import org.joda.time.DateTime;

import java.util.regex.Pattern;

/**
 * 在这里编写类的功能描述
 *
 * @author fangwenhao
 * @created 2017/10/25
 */
public class GenegateDomainUtil {

    private final static String PIC_DOMIAN = PropertyConfigurer.getPropertiesStr("pic.domain");

    private final static String APP_VERSION = PropertyConfigurer.getPropertiesStr("app.version");

    private static String dateTime = "2018-01-01";

    public static String getAppVersion() {
        return APP_VERSION;
    }

    private GenegateDomainUtil(){}

    /**
     * 组合生成域名
     *
     * eg:
     * 1、http://10.33.30.200:80/euc/upload/meetingroom/201804062130133451.jpg  -> http://{picDomain}/euc/upload/meetingroom/201804062130133451.jpg
     * 2、/euc/upload/spaces/201804021713529341.png  -> http://{picDomain}/euc/upload/spaces/201804021713529341.png
     *
     * @param picUrl
     * @return
     */
    public static String generateValidPicUrl(String picUrl){
        if (StringUtil.isEmpty(picUrl)){
            return null;
        }
        String regStr = "(?<=\\/\\/)[^\\/]+";
        Pattern pattern = Pattern.compile(regStr);
        if (pattern.matcher(picUrl).find()){
            String url =  picUrl.replaceFirst(regStr, PIC_DOMIAN);
            if (url.startsWith("https")){
                return url.replaceFirst("https","http");
            }
            return url;
        }else {
            return "http://" + PIC_DOMIAN + picUrl;
        }
    }

    public static String getDateTime() {
        return dateTime;
    }

    public static void setDateTime(String dateTime) {
        GenegateDomainUtil.dateTime = dateTime;
    }

    public static boolean isAvaliable(){
        return new DateTime(GenegateDomainUtil.getDateTime()).isAfter(new DateTime("1970-01-01"));
    }

    public static void main(String[] args) {
        System.out.println(generateValidPicUrl("/euc/upload/meetingroom/201804062130133451.jpg"));
    }
}
